package com.kim.rssreader.utils;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.kim.rssreader.db.DBDAO;

public class Utils {

	public static final String TAG = "Utils";

	/**
	 * 短时显示Toast
	 * 
	 * @param context
	 * @param text
	 */
	public static void toastShort(Context context, String text) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 短时显示Toast
	 * 
	 * @param context
	 * @param text
	 */
	public static void toastShort(Context context, int text) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 长时显示Toast
	 * 
	 * @param context
	 * @param text
	 */
	public static void toastLong(Context context, String text) {
		Toast.makeText(context, text, Toast.LENGTH_LONG).show();
	}

	/**
	 * 长时显示Toast
	 * 
	 * @param context
	 * @param text
	 */
	public static void toastLong(Context context, int text) {
		Toast.makeText(context, text, Toast.LENGTH_LONG).show();
	}

	/**
	 * 获取SharedPreference
	 * 
	 * @param context
	 * @param sharedPreferenceName
	 * @param mode
	 * @return
	 */
	public static SharedPreferences getSharedPreferenceByName(Context context,
			String sharedPreferenceName, int mode) {
		return context.getSharedPreferences(sharedPreferenceName, mode);
	}

	/**
	 * 获取时间日期字体大小
	 * 
	 * @param context
	 * @return
	 */
	public static final String getTimeStart(Context context) {
		SharedPreferences preferences = Utils.getSharedPreferenceByName(
				context, ValueConstant.SETTINGSFILENAME, Context.MODE_PRIVATE);
		int timeSize = 0;
		String timeSizeStrTemp = preferences.getString(ValueConstant.FONTSIZE,
				ValueConstant.timeSizeDefault);
		try {
			timeSize = Integer.parseInt(timeSizeStrTemp);
		} catch (Exception e) {
			timeSize = Integer.parseInt(ValueConstant.timeSizeDefault);
		}
		String timeSizeStr = ValueConstant.timeSizeDefault;
		switch (timeSize) {
		case ValueConstant.EXTREAMLARGE:
			timeSizeStr = "18";
			break;
		case ValueConstant.LARGE:
			timeSizeStr = "16";
			break;
		case ValueConstant.NORMAL:
			timeSizeStr = "14";
			break;
		case ValueConstant.SMALL:
			timeSizeStr = "12";
			break;
		}
		return ValueConstant.TIME_START.replace("$time$", timeSizeStr);
	}

	/**
	 * 获取标题字体大小
	 * 
	 * @param context
	 * @return
	 */
	public static final String getTitleStart(Context context) {
		SharedPreferences preferences = Utils.getSharedPreferenceByName(
				context, ValueConstant.SETTINGSFILENAME, Context.MODE_PRIVATE);
		int titleSize = 0;
		String titleSizeStrTemp = preferences.getString(ValueConstant.FONTSIZE,
				ValueConstant.titleSizeDefault);
		try {
			titleSize = Integer.parseInt(titleSizeStrTemp);
		} catch (Exception e) {
			titleSize = Integer.parseInt(ValueConstant.titleSizeDefault);
		}

		String titleSizeStr = ValueConstant.titleSizeDefault;
		switch (titleSize) {
		case ValueConstant.EXTREAMLARGE:
			titleSizeStr = "19";
			break;
		case ValueConstant.LARGE:
			titleSizeStr = "17";
			break;
		case ValueConstant.NORMAL:
			titleSizeStr = "15";
			break;
		case ValueConstant.SMALL:
			titleSizeStr = "13";
			break;
		}
		return ValueConstant.TITLE_START.replace("$title$", titleSizeStr);
	}

	/**
	 * 获取内容字体大小
	 * 
	 * @param context
	 * @return
	 */
	public static final String getContentStart(Context context) {
		SharedPreferences preferences = Utils.getSharedPreferenceByName(
				context, ValueConstant.SETTINGSFILENAME, Context.MODE_PRIVATE);
		int contentSize = 0;
		String contentSizeStrTemp = preferences.getString(
				ValueConstant.FONTSIZE, ValueConstant.contentSizeDefault);
		try {
			contentSize = Integer.parseInt(contentSizeStrTemp);
		} catch (Exception e) {
			contentSize = Integer.parseInt(ValueConstant.contentSizeDefault);
		}
		String contentSizeStr = ValueConstant.contentSizeDefault;
		String contentLineHeight = ValueConstant.contentLineHeightDefault;

		switch (contentSize) {
		case ValueConstant.EXTREAMLARGE:
			contentSizeStr = "20";
			contentLineHeight = "29";
			break;
		case ValueConstant.LARGE:
			contentSizeStr = "18";
			contentLineHeight = "27";
			break;
		case ValueConstant.NORMAL:
			contentSizeStr = "16";
			contentLineHeight = "25";
			break;
		case ValueConstant.SMALL:
			contentSizeStr = "14";
			contentLineHeight = "23";
			break;
		}
		return ValueConstant.CONTENT_START.replace("$content$", contentSizeStr)
				.replace("$line-height$", contentLineHeight);
	}

	/**
	 * 获取原文连接
	 * 
	 * @param orignalLink
	 * @return
	 */
	public static final String getOrignalContentLink(String orignalLink) {
		return ValueConstant.VIEW_ORIGNAL_CONTENT.replace("$link", orignalLink);
	}

	/**
	 * 组建内容
	 * 
	 * @param context
	 * @param orignalLink
	 * @param contentOrignal
	 * @param title
	 * @param time
	 * @return
	 */
	public static String buildContent(Context context, String orignalLink,
			String contentOrignal, String title, String time) {

		String content = contentOrignal;

		/**
		 * 无图浏览模式
		 */
		if (SharedPreferencesUtils.getValueBoolean(ValueConstant.PURETEXTMODE,
				ValueConstant.SETTINGSFILENAME, context)) {
			content = ImgFilter.fliter(content, context);
		}

		content = HtmlTagFilter.filterFlagA(content);
		content = HtmlTagFilter.filterFlagFont(content);
		content = HtmlTagFilter.filterFlagIndent(content);
		content = HtmlTagFilter.filterFlagPSpace(content);
		content = HtmlTagFilter.filterFlagImg(content);
		content = HtmlTagFilter.filterFlagInvalidSymbol(content);

		content = ValueConstant.META + ValueConstant.BODY_START
				+ getTitleStart(context) + title + ValueConstant.TITLE_END
				+ getTimeStart(context) + time + ValueConstant.TIME_END
				+ ValueConstant.HR + getContentStart(context) + content
				+ ValueConstant.CONATENT_END
				+ getOrignalContentLink(orignalLink) + ValueConstant.BODY_END;

		return content;
	}

	/**
	 * 文章内容中图片地址使用本地地址
	 * 
	 * @param context
	 * @param dbdao
	 * @param contentUrl
	 * @param contentOrignal
	 * @param title
	 * @param time
	 * @return
	 */
	public static String buildContentWidthLocalImageUrl(Context context,
			DBDAO dbdao, String contentUrl, String contentOrignal,
			String title, String time) {

		String content = buildContent(context, contentUrl, contentOrignal,
				title, time);

		/**
		 * 无图浏览模式
		 */
		if (SharedPreferencesUtils.getValueBoolean(ValueConstant.PURETEXTMODE,
				ValueConstant.SETTINGSFILENAME, context)) {
			return content;
		}

		// 将内容中图片网络地址替换为本地地址
		List<String> imageUrlArray = dbdao.getContentImageUrls(contentUrl);
		if (imageUrlArray != null && imageUrlArray.size() > 0
				&& null != content) {
			for (String imageUrl : imageUrlArray) {
				String localPath = DrawableUtils.getImageSavePath(imageUrl);
				if (FileUtils.fileExist(localPath)) {
					content = content.replaceAll(imageUrl,
							ValueConstant.LOCAL_PATH_SCHEMA + localPath);
				}
			}
		}
		return content;
	}

	/**
	 * 截取内容
	 * 
	 * @param content
	 * @param columnFlagStarter
	 * @param columnFlagEnd
	 * @return
	 */
	public static final String interceptedContent(String content,
			String columnFlagStarter, String columnFlagEnd) {
		if (content == null) {
			return null;
		}

		if (columnFlagStarter == null || columnFlagEnd == null) {
			return content;
		}

		int contentStart = content.indexOf(columnFlagStarter);
		int contentEnd = content.indexOf(columnFlagEnd, contentStart);
		if (contentStart > 0 && contentEnd > contentStart) {
			content = content.substring(contentStart, contentEnd);
		}

		return content;
	}

	/**
	 * 抓取内容
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static final String fetchData(String url) throws IOException {
		return NetUtil.startFetchData(NetUtil.getHttpURLConnection(url));
	}

}
